package es.mgil.ejercicios.clase;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class MultiToquePantalla extends Activity implements OnTouchListener {
	private StringBuilder mensajes = new StringBuilder();
	private TextView visor;
	private final int MAX = 10;
	private String[] toque = new String[MAX];
	private int[] x = new int[MAX];
	private int[] y = new int[MAX];
	private float[] area = new float[MAX];
	private float[] pressure = new float[MAX];

	private void updateScreen() {
		mensajes.setLength(0);
		for (int i = 0; i < MAX; i++) {
			mensajes.append((i + 1) + ": ");
			mensajes.append(toque[i] + ", ");
			mensajes.append("x: " + x[i] + ", ");
			mensajes.append("y: " + y[i] + ", ");
			mensajes.append("Size: " + area[i] + ", ");
			mensajes.append("Press: " + pressure[i]);
			mensajes.append("\n");
		}
		visor.setText(mensajes.toString());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		visor = new TextView(this);
		visor.setText("Toca la pantalla y arrastra con VARIOS dedo");
		visor.setOnTouchListener(this);
		setContentView(visor);
		for (int i = 0; i < MAX; i++) {
			toque[i] = "No";
		}
		updateScreen();
	}

	@SuppressWarnings("deprecation")
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int action = event.getAction() & MotionEvent.ACTION_MASK;
		int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
		int pointerId = event.getPointerId(pointerIndex);

		switch (action) {
		case MotionEvent.ACTION_DOWN:
		case MotionEvent.ACTION_POINTER_DOWN:
			toque[pointerId] = "Down";
			x[pointerId] = (int) event.getX(pointerIndex);
			y[pointerId] = (int) event.getY(pointerIndex);
			area[pointerId] = event.getSize(pointerIndex);
			pressure[pointerId] = event.getPressure(pointerIndex);
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_POINTER_UP:
		case MotionEvent.ACTION_CANCEL:
			toque[pointerId] = "Up";
			x[pointerId] = (int) event.getX(pointerIndex);
			y[pointerId] = (int) event.getY(pointerIndex);
			area[pointerId] = event.getSize(pointerIndex);
			pressure[pointerId] = event.getPressure(pointerIndex);
			break;
		case MotionEvent.ACTION_MOVE:
			int fingerCount = event.getPointerCount();
			for (int i = 0; i < fingerCount; i++) {
				pointerIndex = i;
				pointerId = event.getPointerId(pointerIndex);
				toque[pointerId] = "Move";
				x[pointerId] = (int) event.getX(pointerIndex);
				y[pointerId] = (int) event.getY(pointerIndex);
				area[pointerId] = event.getSize(pointerIndex);
				pressure[pointerId] = event.getPressure(pointerIndex);
			}
			break;
		}

		updateScreen();

		return true;
	}
}
